#include "zlang.h"

#define ZLANG_OBJECT_gravity_util	"gravity_util"

#define g				9.8

struct ZlangDirectProperty_gravity_util
{
	double		G ;
} ;

ZlangInvokeFunction ZlangInvokeFunction_gravity_util_CalcGravity_double;
int ZlangInvokeFunction_gravity_util_CalcGravity_double( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_gravity_util	*gravity_util_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	double					m ;
	double					G ;
	
	CallRuntimeFunction_double_GetDoubleValue( rt , in1 , & m );
	
	/* G=mg */
	G = m * g ;
	
	CallRuntimeFunction_double_SetDoubleValue( rt , out1 , G );
	
	gravity_util_direct_prop->G = G ;
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_gravity_util_GetLastResult;
int ZlangInvokeFunction_gravity_util_GetLastResult( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_gravity_util	*gravity_util_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_double_SetDoubleValue( rt , out1 , gravity_util_direct_prop->G );
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_gravity_util;
void *ZlangCreateDirectProperty_gravity_util( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_gravity_util	*gravity_util_prop = NULL ;
	
	gravity_util_prop = (struct ZlangDirectProperty_gravity_util *)malloc( sizeof(struct ZlangDirectProperty_gravity_util) ) ;
	if( gravity_util_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( gravity_util_prop , 0x00 , sizeof(struct ZlangDirectProperty_gravity_util) );
	
	return gravity_util_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_gravity_util;
void ZlangDestroyDirectProperty_gravity_util( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_gravity_util	*gravity_util_direct_prop = GetObjectDirectProperty(obj) ;
	
	free( gravity_util_direct_prop );
	
	return;
}

static struct ZlangDirectFunctions direct_funcs_gravity_util =
	{
		ZLANG_OBJECT_gravity_util , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_gravity_util , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_gravity_util , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
	} ;

ZlangImportObjectsFunction ZlangImportObjects;
int ZlangImportObjects( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangObject	*prop = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_gravity_util , & direct_funcs_gravity_util , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return GetRuntimeErrorNo(rt);
	}
	
	prop = AddPropertyInObject( rt , obj , GetDoubleObjectInRuntimeObjectsHeap(rt) , "g") ;
	if( prop == NULL )
		return GetRuntimeErrorNo(rt);
	CallRuntimeFunction_double_SetDoubleValue( rt , prop , g );
	SetConstantObject( prop );
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_gravity_util_CalcGravity_double , ZLANG_OBJECT_double , "CalcGravity" , ZLANG_OBJECT_double,NULL , NULL ) ;
	if( func == NULL )
		return GetRuntimeErrorNo(rt);
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_gravity_util_GetLastResult , ZLANG_OBJECT_double , "GetLastResult" , NULL ) ;
	if( func == NULL )
		return GetRuntimeErrorNo(rt);
	
	return 0 ;
}

